import Vue from 'vue';
import VueRouter, {RouteConfig} from 'vue-router';
import Tally from '@/views/Tally.vue';
import Charts from '@/views/Charts.vue';
import Details from '@/views/Details.vue';
import NotFound from '@/views/NotFound.vue';

Vue.use(VueRouter);

const routes: Array<RouteConfig> = [
    {
        path: '/',
        redirect: '/tally'
    },
    {
        path: '/tally',
        component: Tally,
        meta: {
            title: '记一笔'
        },
    },
    {
        path: '/charts',
        component: Charts,
        meta: {
            title: '统计'
        }
    },
    {
        path: '/details',
        component: Details,
        meta: {
            title: '明细'
        }
    },
    {
        path: '*',
        component: NotFound
    }
];

const router = new VueRouter({
    routes
});

let toURL: string;
router.beforeEach((to, from, next) => {
    if (toURL === to.path) {
        next();
    }
    if (to.path !== from.path) {
        toURL = to.path;
        router.replace(to.path).catch(err => err);
        next();
    }
    //判断是否有标题
    if (to.meta.title) {
        document.title = to.meta.title
        next();
    }
});


export default router;
